package ph.gov.marikina.mcsmtms.rest.webservices;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

import ph.gov.marikina.mcsmtms.domain.User;
import ph.gov.marikina.mcsmtms.services.UserService;

@Controller
public class UserController {

	@Autowired
	private View jsonView;

	@Autowired
	private UserService userService;

	@RequestMapping(value = "/users/getLoggedUserName/", method = RequestMethod.GET)
	public ModelAndView getLoggedUserName() {

		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		String name = auth.getName(); // get logged in username

		// model.addAttribute("username", name);
		ModelAndView mav = new ModelAndView(jsonView,
				BindingResult.MODEL_KEY_PREFIX + "getLoggedUserName",
				name);
		return mav;
	}

	@RequestMapping(value = "/users/{username}", method = RequestMethod.GET)
	public ModelAndView getUser( @PathVariable( "username" ) String username) {
		User user = userService.getUser(username);
		ModelAndView mav = new ModelAndView(jsonView, "user", user);
		return mav;
	}

}
